package com.imooc.food.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * ProjectName food
 *
 * @author xieyucan
 * <br>CreateDate 2022/9/22 17:23
 */
@Slf4j
@Configuration
public class RabbitConfig {

    //-------------------------------practice---------------------------------//
    @Bean
    public Exchange exchange1()
    {
        return new DirectExchange("exchange.my.practice");
    }
    @Bean
    public Queue queue1()
    {
        Map<String,Object> map=new HashMap<>(8);
        //配置过期时间和死信队列
        map.put("x-message-ttl",15000);
        map.put("x-dead-letter-exchange","exchange.my.dead");
        return new Queue("my.queue",true,false,false,map);
    }
    @Bean
    public Binding binding1()
    {
        return new Binding(
                "my.queue",
                Binding.DestinationType.QUEUE,
                "exchange.my.practice",
                "key.my",
                null
        );
    }

    //-------------------------------死信队列---------------------------------//
    @Bean
    public Exchange exchange2()
    {
        return new TopicExchange("exchange.my.dead");
    }
    @Bean
    public Queue queue2()
    {
        return new Queue("my.dead");
    }
    @Bean
    public Binding binding2()
    {
        return new Binding(
                "my.dead",
                Binding.DestinationType.QUEUE,
                "exchange.my.dead",
                "key.#",
                null
        );
    }


    //-------------------------------fanout1---------------------------------//

    @Bean
    public Exchange exchange3()
    {
        return new FanoutExchange("exchange.my.fanout");
    }
    @Bean
    public Queue queue3()
    {
        return new Queue("my.fanout");
    }
    @Bean
    public Binding binding3()
    {
        return new Binding(
                "my.fanout",
                Binding.DestinationType.QUEUE,
                "exchange.my.fanout",
                "key.fanout",
                null
        );
    }

    //-------------------------------fanout2---------------------------------//
    @Bean
    public Queue queue4()
    {
        return new Queue("my.fanout1");
    }
    @Bean
    public Binding binding4()
    {
        return new Binding(
                "my.fanout1",
                Binding.DestinationType.QUEUE,
                "exchange.my.fanout",
                "key.fanout1",
                null
        );
    }

//    @Bean
//    public ConnectionFactory connectionFactory()
//    {
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
//        connectionFactory.setHost("43.138.244.235");
//        connectionFactory.setPort(5672);
//        connectionFactory.setUsername("admin");
//        connectionFactory.setPassword("1234");
//        connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
//        connectionFactory.setPublisherReturns(true);
//        //这里留空
//        return connectionFactory;
//    }
//
//    @Bean
//    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory)
//    {
//        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
//        rabbitAdmin.setAutoStartup(true);
//        return rabbitAdmin;
//    }
//
//    @Bean
//    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory)
//    {
//        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
//        rabbitTemplate.setMandatory(true);
//        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//            @Override
//            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//                log.info("correlationData:{},ack:{},cause:{}",correlationData,ack,cause);
//            }
//        });
//        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
//            @Override
//            public void returnedMessage(ReturnedMessage returned) {
//                log.info("returned:{}",returned);
//            }
//        });
//        return rabbitTemplate;
//    }
//
//
//    @Bean
//    public RabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory)
//    {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        factory.setConnectionFactory(connectionFactory);
//        return factory;
//    }

}
